package com.cn.qgx428.orderingmeal.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.adapter.CollectionFoodAdapter;
import com.cn.qgx428.orderingmeal.adapter.CollectionShopAdapter;
import com.cn.qgx428.orderingmeal.myClass.CollectionFood;
import com.cn.qgx428.orderingmeal.myClass.CollectionShop;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.User;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectionFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "CollectionFragment";

    private View view = null;
    private MyKeyInfo keyInfo;

    private LinearLayoutManager linearLayoutManager;
    private RecyclerView recycler;
    private CollectionShopAdapter shopAdapter;
    private CollectionFoodAdapter foodAdapter;

    private LinearLayout collection_shopBtn;
    private LinearLayout collection_foodBtn;
    private ImageView collection_foodBtnImg;
    private ImageView collection_shopBtnImg;

    private int FIRST_CLICK = 1;
    private int NOT_FIRST_CLICK = 0;
    private int FIRST_CLICK_COLLETION_FOOD = FIRST_CLICK;

    public CollectionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_collection, container, false);
        Bmob.initialize(getActivity(),"1b925fe2ccbb402f4034a3e28c31ad93");

        init();
        getInitData();
        return view;
    }

    private void getInitData() {
        //获得收藏店铺
        User user = new User();
        user.setObjectId(keyInfo.getUserID());
        BmobQuery<CollectionShop> query = new BmobQuery<CollectionShop>();
        query.addWhereEqualTo("user",new BmobPointer(user));
        query.include("shop");
        query.findObjects(new FindListener<CollectionShop>() {
            @Override
            public void done(List<CollectionShop> list, BmobException e) {
                if (e == null){
                    Log.d(TAG, "获取收藏店铺: " +list.size());
                    shopAdapter = new CollectionShopAdapter(getActivity(),list,keyInfo);
                    recycler.setAdapter(shopAdapter);
                } else{
                    Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "获得初始数据"+e.getErrorCode()+e.getMessage());
                }
            }
        });
    }

    private void init() {
        keyInfo = (MyKeyInfo) getActivity().getIntent().getSerializableExtra("keyInfo");

        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        recycler = (RecyclerView) view.findViewById(R.id.Collection_recyclerView);
        recycler.setLayoutManager(linearLayoutManager);

        collection_shopBtn = (LinearLayout) view.findViewById(R.id.collection_shopLayoutBtn);
        collection_foodBtn = (LinearLayout) view.findViewById(R.id.collecton_foodLayoutBtn);
        collection_shopBtnImg = (ImageView) view.findViewById(R.id.collection_shopBtnImg);
        collection_foodBtnImg = (ImageView) view.findViewById(R.id.collection_foodBtnImg);
        collection_shopBtn.setOnClickListener(this);
        collection_foodBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.collection_shopLayoutBtn:
                changeRecyler(1);
                collection_shopBtnImg.setImageResource(R.drawable.collection_shop1);
                collection_foodBtnImg.setImageResource(R.drawable.collection_food);
                break;
            case R.id.collecton_foodLayoutBtn:
                collection_shopBtnImg.setImageResource(R.drawable.collection_shop);
                collection_foodBtnImg.setImageResource(R.drawable.collection_food1);
                changeRecyler(2);
                break;
        }

    }

    private void changeRecyler(int way) {
        //通过按钮变换更换适配器，展现不同页面
        if (way == 1 ){
            recycler.setAdapter(shopAdapter);
        } else if(way == 2){
            if (FIRST_CLICK_COLLETION_FOOD == FIRST_CLICK){
                //获取食物收藏初次数据
                BmobQuery<CollectionFood> query = new BmobQuery<CollectionFood>();
                User user = new User();
                user.setObjectId(keyInfo.getUserID());
                query.addWhereEqualTo("user",new BmobPointer(user));
                query.include("shop,food");
                query.findObjects(new FindListener<CollectionFood>() {
                    @Override
                    public void done(List<CollectionFood> list, BmobException e) {
                        if (e == null){
                            Log.d(TAG, "获取食物收藏初次数据返回数据: "+list.size());
                            foodAdapter = new CollectionFoodAdapter(getActivity(),getActivity(), list, keyInfo);
                            recycler.setAdapter(foodAdapter);
                            FIRST_CLICK_COLLETION_FOOD = NOT_FIRST_CLICK;
                        }  else{
                            Toast.makeText(getActivity(), "服务器错误", Toast.LENGTH_SHORT).show();
                            Log.d(TAG, "获取食物收藏初次数据: "+e.getErrorCode()+e.getMessage());
                        }
                    }
                });
            } else {
                recycler.setAdapter(foodAdapter);
            }
        }
    }
}
